<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Services\ArticleService;
use App\Services\ChatService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChatController extends Controller{


    public $request;
    public $ChatService;

    public function __construct(Request $request, ChatService $ChatService){
        $this->request = $request;
        $this->ChatService = $ChatService;
    }



    //聊天室
    public function index(){
        $fromid = $_GET['fromid'];
        $toid = $_GET['toid'];
        return view("home.chat.index")->with('fromid',$fromid)->with('toid',$toid);
    }


    public function save_message(){
        $data = $this->request->all();
        $data = $data['message'];
        $ConData['fromid'] = $data['fromid'];
        $ConData['fromname'] = self::get_chat_name($data['fromid']);
        $ConData['toid'] = $data['toid'];
        $ConData['toname'] = self::get_chat_name($data['toid']);
        $ConData['content'] = $data['data'];
        $ConData['time'] = $data['time'];
        $ConData['isread'] = $data['isread'];
        $ConData['type'] = 1;
        $req_result = $this->ChatService->saveMessage($ConData);
//        if($req_result['success']){
//
//        }else{
////            return back()->withErrors([$req_result['msg']])->withInput();
//        }
    }

    public function get_chat_name($uid){
        $userinfo =  DB::table('chat_user')->select('nickname')->where('id',$uid)->first();
        return $userinfo->nickname;
    }

    public function get_head(){
        $data       = $this->request->all();
        $frominfo =  DB::table('chat_user')->where('id',$data['fromid'])->select('headimgurl')->first();
        $toinfo =  DB::table('chat_user')->where('id',$data['toid'])->select('headimgurl')->first();
        return [
            'from_head'=>$frominfo->headimgurl,
            'to_head'=>$toinfo->headimgurl,
        ];
    }
    public function get_name(){
        $data       = $this->request->all();

        $userinfo =  DB::table('chat_user')->select('nickname')->where('id',$data['uid'])->first();
        return $userinfo->nickname;
    }

    //从数据库中读取数据
    public function load_message(){
        $data       = $this->request->all();
        $fromid = $data['fromid'];
        $toid = $data['toid'];
        $message =  DB::table('chat_communication')->where(['fromid'=>$fromid,'toid'=>$toid])->orWhere(['fromid'=>$toid,'toid'=>$fromid])->orderBy('id')->get()->toArray();
        return $message;
//        $count =  DB::table('chat_communication')->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->count('id');
//        if($count>=10){
//            $message =    DB::table('chat_communication')->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->limit($count-10,10)->order('id')->select();
//        }else{
//            $message =   DB::table('chat_communication')->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->order('id')->select();
//        }
    }

    //保存图片
    public function upload_image(){
        $data       = $this->request->all();
        $fromid = $data['fromid'];
        $toid = $data['toid'];
        $online = $data['online'];
        $pic = $data['img'];
        //上传文件夹的路径
        $dirname = 'Style/upload/img/chat/';
        // 获取上传的文件的后缀
        $ext = $pic->getClientOriginalExtension();
        // 拼装出你需要使用的文件名
        $picname = trim($dirname,'.').time().rand(100000,999999).'.'.$ext;
        // 移动临时文件，生成新文件到指定目录下
        $re =$pic->move($dirname,$picname);
        if($re){
            $re_data['content'] = $picname;
            $re_data['fromid'] = $fromid;
            $re_data['toid'] = $toid;
            $re_data['fromname'] = self::get_chat_name($data['fromid']);
            $re_data['toname'] = self::get_chat_name(($data['toid']));
            $re_data['time'] = time();
            $re_data['isread'] = $online;
            $re_data['type'] = 2;
            $message_id = DB::table('chat_communication')->insertGetId($re_data);
            if($message_id){
                return['status'=>'ok','img_name'=>$picname];
            }else{
                return ['status'=>'false'];
            }
        }
    }






}
